﻿using System;
using System.Windows;
using ClientBankWPF.Entities;
using ClientBankWPF.Service;
using CryptoModule;
using DevExpress.Xpf.Core;

namespace ClientBankWPF
{
    /// <summary>
    /// Interaction logic for LocalUserAddWindow.xaml
    /// </summary>
    public partial class LocalUserAddWindow
    {
        private readonly DataAccessService dataAccessService;

        public LocalUser LocalUser { set; get; }

        public LocalUserAddWindow()
        {
            InitializeComponent();
        }

        public LocalUserAddWindow(DataAccessService dataAccessService)
        {
            InitializeComponent();
            this.dataAccessService = dataAccessService;
            comboBoxRole.ItemsSource = dataAccessService.GetRoles();
            comboBoxRole.DisplayMember = "RoleName";
            comboBoxRole.SelectedIndex = 0;
        }

        private void ButtonCreateLocalUserClick(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(textBoxUserName.Text))
            {
                LocalUser = dataAccessService.GetLocalUser(textBoxUserName.Text);
                if (LocalUser == null)
                {
                    if (!String.IsNullOrEmpty(passwordBoxLocalUserPassword.Password))
                    {
                        LocalUser = new LocalUser
                        {
                            Id = Guid.NewGuid(),
                            UserName = textBoxUserName.Text,
                            Password = MD5Crypto.MD5Encode(passwordBoxLocalUserPassword.Password),
                            IdRole = ((Role)comboBoxRole.SelectedItemValue).Id,
                            IsActive = true
                        };
                        string result = dataAccessService.AddLocalUser(LocalUser);
                        if (!String.IsNullOrEmpty(result))
                        {
                            DXMessageBox.Show(result, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        else
                        {
                            //Close();
                            //((ObservableCollection<LocalUser>)GridControlLocalUsers.DataContext).Add(localUser);
                            DXMessageBox.Show("Пользователь добавлен", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxUserName.Text = "";
                            passwordBoxLocalUserPassword.Password = "";
                            comboBoxRole.SelectedIndex = 0;
                            Close();
                        }
                    }
                    else
                    {
                        DXMessageBox.Show("Пароль не введён", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    DXMessageBox.Show("Такой пользователь уже существует", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    LocalUser = null;
                }
            }
            else
            {
                DXMessageBox.Show("Имя пользователя не введено", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}